package cn.sccl.test.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import cn.sccl.service.UserService;
import cn.sccl.service.UserServiceImpl;

/**
 * @author 饶爽
 * @time 2019年9月2日 下午11:56:08
 * @version 1.0.0
 * 观光代码=>动态代理(JDK的Proxy)
*/
public class UserServiceProxyFactory implements InvocationHandler {

    private UserService us;
    public UserServiceProxyFactory(UserService us) {
        super();
        this.us=us;
    }
    public UserService getUserServiceProxy() {
        //生成动态代理对象
        UserService usProxy = (UserService) Proxy.newProxyInstance(UserService.class.getClassLoader(),
                UserServiceImpl.class.getInterfaces(), this);
        
        return usProxy;
    }
    
    @Override
    public Object invoke(Object obj, Method method, Object[] objs) throws Throwable {
        System.out.println("打开事务!");
        Object invoke = method.invoke(us, objs);
        System.out.println("提交事务!");
        return invoke;
    }

}
